﻿using DotNetCore.CAP;
using Hangfire;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X23.ApplicationJob;
using X23.EFCore.Repository;
using X23.Model.User;
using X23.Util.SerializationUtil;

namespace X23.Application.TestManagement
{
    public class TestManagementService : ITestManagementService, ICapSubscribe
    {
        private readonly IRepository<User> _userRepository;

        public TestManagementService(IRepository<User> userRepository)
        {
            _userRepository = userRepository;
        }
        [CapSubscribe("TestCapSubscribeByService")]
        public async Task TestCapSubscribeByService(string str)
        {
            var data = await _userRepository.GetByIdAsync(1);
            Console.WriteLine($"{str}+{data.ToJson()}");
        }
        [Job("TestAsyncJobKey", "TestAsyncJob", "0 0/1 * * * ?", "TestAsyncJob测试任务", "每分钟")]
        public async Task TestAsyncJob()
        {
            Console.WriteLine($"async_{DateTime.Now}____{ (await _userRepository.GetByIdAsync(1)).ToJson()}");

            //同时执行带参数的方法
            BackgroundJob.Enqueue(() => TestAsyncJobWitPara("Enqueue"));
        }

        public async Task TestAsyncJobWitPara(string str)
        {
            var json = (await _userRepository.GetByIdAsync(1)).ToJson();
            Console.WriteLine(str+ json);
        }

        [Job("TestJobKey", "TestJob", "0 0/1 * * * ?", "TestJob测试任务", "每分钟")]
        public void TestJob()
        {
            Console.WriteLine($"===={DateTime.Now}____{ _userRepository.GetByIdAsync(1).Result.ToJson()}");
        }
    }
}
